
'use strict';

/**
	Part of Zombie Navigator
	Copyright © 2015  Zombie Navigator Developers
	
	@file
	@module
	@license AGPL-3.0+
*/


/* Private properties */

/**
	Protect Function.prototype.toString() against modification?
	@private
*/
Object.freeze(Object.getPrototypeOf(function () {}));
const _functionToString = Object.freeze(
	Object.getPrototypeOf(function () {}).toString
);


/* Exported APIs */

/**
	Convert a function into an evaluatable string in a secure way
	@public
*/
const serializeFunction = (f) => {
	if ('function' != typeof f) {
		throw new TypeError('Not a function');
	}
	
	/*
		Use the protected toString() function to serialize the callback
		The resulting callback is always one valid function expression.
		(Should be guaranteed by the ECMAScript standard)
	*/
	return _functionToString.call(f) + '';
};


/* Exports */
exports.serializeFunction = serializeFunction;


// vim: ts=4 noet ai

